package net.caixiaomi.info.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.BaseAlertDialog;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.info.widgets.DefLoading;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;


/**
 * 忘记密码
 */

public class ForgetPasswordActivity extends BaseActivity implements TextWatcher {
    @BindView(R.id.phone)
    EditText mPhone;
    @BindView(R.id.phone_state)
    View mPhoneState;
    @BindView(R.id.btn_delete)
    View mBtnDelete;

    @OnClick(R.id.btn_delete)
    void toDelete() {
        mPhone.setText("");
    }

    @OnClick(R.id.btn_next)
    void toNext() {
        final String phone = mPhone.getText().toString().trim();
        if (!AppHelper.checkPhoneAvailable(phone)) {
            ToastUtil.s(R.string.C_PHONE_INVALID);
            return;
        }
        AppHelper.sendStatistics("wjmmxiayibu");
        mLoading.show();
        JSONObject params = new JSONObject();
        params.put("mobileNumber", phone);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<String>> validateMobile = requestService.validateMobile(params);
        validateMobile.enqueue(new ResponseCallback<BaseCallModel<String>>(this) {
            @Override
            public void onSuccess(BaseCallModel data) {
                mLoading.dismiss();
                toSetPassword(phone);
            }

            @Override
            public void onFail(ResponseError error) {
                mLoading.dismiss();
                if (error.getCode() >= 300000 && error.getCode() <= 310000) {
                    if (error.getCode() == 301014) {
                        showHasNotRegitst(error.getMessage());
                    } else {
                        ToastUtil.s(error.getMessage());
                    }
                }
            }

            @Override
            public void onError() {
                mLoading.dismiss();
            }
        });

    }

    @Override
    protected int layout() {
        return R.layout.act_forget_password;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoading = new DefLoading(this);
        mPhone.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        mPhone.addTextChangedListener(this);
        mPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.sendStatistics("wjmmshushouji");
            }
        });
        mPhone.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AppHelper.sendStatistics("wjmmshushouji");
                return false;
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onPostEvent(CommonEvent event) {
        switch (event.getWhat()) {
            case EventWhat.EVENT_RESET_PASSWORD:
                super.onBackPressed();
                break;

        }
    }

    private void toRegitst() {
        Intent intent = new Intent(this, RegistActivity.class);
        startActivity(intent);
    }

    private void toSetPassword(String phone) {
        Intent intent = new Intent(this, SetPasswordActivity.class);
        intent.putExtra("phone", phone);
        startActivity(intent);
    }

    private void showHasNotRegitst(String message) {
        BaseAlertDialog builder = new BaseAlertDialog(this);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.C_GOING_TO_REGIST, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toRegitst();
                onBackPressed();
            }
        });

        builder.show();

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String text = s.toString().trim();
        mBtnDelete.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        mPhoneState.setSelected(!TextUtils.isEmpty(text));
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onBackPressed() {
        AppHelper.sendStatistics("wjmmfanhui");
        super.onBackPressed();
    }
}
